<!doctype html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta charset="utf-8" />
    <title>Shopping Cart
 - yiiext</title>
    <style type="text/css">
    /* Meyer's reset */
    html, body, div, span, applet, object, iframe,
    h1, h2, h3, h4, h5, h6, p, blockquote, pre,
    a, abbr, acronym, address, big, cite, code,
    del, dfn, em, font, img, ins, kbd, q, s, samp,
    small, strike, strong, sub, sup, tt, var,
    b, u, i, center,
    dl, dt, dd, ol, ul, li,
    fieldset, form, label, legend,
    table, caption, tbody, tfoot, thead, tr, th, td {
        margin: 0; padding: 0;
        border: 0; outline: 0;
        font-size: 100%;
        vertical-align: baseline;
        background: transparent;
    }
    body { line-height: 1; }
    ol, ul { list-style: none; }
    blockquote, q { quotes: none; }
    blockquote:before, blockquote:after,
    q:before, q:after { content: ''; content: none; }

    /* remember to define focus styles! */
    :focus { outline: 0; }

    /* remember to highlight inserts somehow! */
    ins { text-decoration: none; }
    del { text-decoration: line-through; }

    /* tables still need 'cellspacing="0"' in the markup */
    table { border-collapse: collapse; border-spacing: 0; }

/*
yiiext CSS
Alexander Makarov, sam@rmcreative.ru
*/
body {
    font: normal 10pt Arial, Helvetica, sans-serif;
    background: #fff;
    color: #555;
    text-align: center;
}

a, a:visited {
    color: #306495;
    text-decoration: none;
}

a:hover {
    color: #73471b;
    text-decoration: underline;
}

h1, h2, h3, h4, h5, h6 {
    font-weight: bold;
    font-family: Calibri, Helvetica, Arial, sans-serif;
}

h1 {
    font-size: 1.6em;
    margin: 1em 0 .5em;
}

h2 {
    font-size: 1.5em;
    margin: 1.07em 0 .535em;
}

h3 {
    font-size: 1.4em;
    margin: 1.14em 0 .57em;
}

h4 {
    font-size: 1.3em;
    margin: 1.23em 0 .615em;
}

h5 {
    font-size: 1.2em;
    margin: 1.33em 0 .67em;
}

h6 {
    font-size: 1.1em;
    margin: 1.6em 0 .8em;
}

::selection {
    background: #d3dfee;
    color: #000;
}

::-moz-selection {
    background: #d3dfee;
    color: #000;
}

acronym, abbr {
    cursor: help;
}

label {
    cursor: pointer;
}

hr {
    height: 1px;
    color: #ccc;
    background-color: #ccc;
    border: none;
}

#content {
    width: 980px;
    margin: 0 auto;
    text-align: left;

    background: #fff;
}

/* guide content */
div.image {
    -moz-border-radius: 7px;
    -webkit-border-radius: 7px;
    -khtml-border-radius: 7px;
    border-radius: 7px;
    margin: 10px 0;
    border: 3px solid #eee;
    text-align: center;
}

div.image > p {
    background: #eee;
    margin: 0;
    font-weight: bold;
    display: block;
}

#content ul {
    list-style: disc inside;
}

#content ol {
    list-style: decimal inside;
}

#content li {
    line-height: 160%;
}

#content ul li li {
    list-style-type: circle;
}

#content ul, #content ol {
    margin: 0 0 1em 2em;
}

#content ul p, #content ol p {
    display: inline;
}

#content p {
    line-height: 160%;
    margin-bottom: 1em;
}

span.type {
    float: left;
    font-size: 1em;
    padding-right: 0.5em;
    font-weight: bold;
}

/* code */
pre {
    display: block;
    padding: 1em;
    background: #fcfcfc;
    border-top: 1px solid #eee;
    border-bottom: 1px solid #eee;
    font-family: Consolas, "Courier New", Courier, mono;
    margin: 1em 0;
    font-size: 10pt;
    line-height: 130%;
}

code {
    border-bottom: 1px dotted #ccc;
    font-weight: bold;
    color: #666;
}

/* --- code highlighting --- */

/* html-code */
.html-hl-brackets,
.html-hl-reserved {
    color: #0000e6;
}

.html-hl-var,
.html-hl-code,
.html-hl-quotes,
.html-hl-string {
    color: #009933;
}

/* php-code */
.php-hl-inlinetags {
    font-weight: bold;
}

.php-hl-comment {
    color: #777;
}

.php-hl-quotes,
.php-hl-string {
    color: #009933;
}

.php-hl-var {
    color: #6d3206;
}

.php-hl-reserved {
    color: #00e;
}

/* css-code */
.css-hl-identifier {
    color: #007c00;
    font-weight: bold;
}

.css-hl-reserved {
    color: #0000e6;
}

/* css-sql */
.sql-hl-reserved {
    color: #0000e6;
}

.sql-hl-identifier {
    color: #007c00;
}

.sql-hl-var {
    color: #0000e6;
}

.sql-hl-quotes, .sql-hl-string {
    color: #007c00;
}
    </style>
</head>
<body>
    <div id="content">
        <h1>Shopping Cart</h1>

<p>Компонент для реализации корзины моделей.</p>

<p>Корзина - объект контейнер, для хранения коллекции позиций, и методами для работы с этой коллекцией.</p>

<p>Состояние корзины между запросами хранится в сессии пользователя.</p>

<h2>Установка и настройка</h2>

<p>В <code>protected/config/main.php</code> добавить:</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-quotes">'</span><span class="php-hl-string">components</span><span class="php-hl-quotes">'</span><span class="php-hl-code"> =&gt; </span><span class="php-hl-reserved">array</span><span class="php-hl-brackets">(</span>
  <span class="php-hl-quotes">'</span><span class="php-hl-string">shoppingCart</span><span class="php-hl-quotes">'</span><span class="php-hl-code"> =&gt;
    </span><span class="php-hl-reserved">array</span><span class="php-hl-brackets">(</span>
        <span class="php-hl-quotes">'</span><span class="php-hl-string">class</span><span class="php-hl-quotes">'</span><span class="php-hl-code"> =&gt; </span><span class="php-hl-quotes">'</span><span class="php-hl-string">ext.shoppingCart.EShoppingCart</span><span class="php-hl-quotes">'</span><span class="php-hl-code">,
    </span><span class="php-hl-brackets">)</span><span class="php-hl-code">,
</span><span class="php-hl-brackets">)</span></pre></div></div>

<h2>Подготавливаем модель</h2>

<p>Модели, которым необходимо дать возможность добавления в корзину,
должны реализовать интерфейс <code>IECartPosition</code>:</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-reserved">class</span> <span class="php-hl-identifier">Book</span> <span class="php-hl-reserved">extends</span> <span class="php-hl-identifier">CActiveRecord</span> <span class="php-hl-reserved">implements</span> <span class="php-hl-identifier">IECartPosition</span> <span class="php-hl-brackets">{</span>
    <span class="php-hl-reserved">public</span> <span class="php-hl-reserved">static</span> <span class="php-hl-reserved">function</span> <span class="php-hl-identifier">model</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$className</span><span class="php-hl-code"> = </span><span class="php-hl-reserved">__CLASS__</span><span class="php-hl-brackets">)</span> <span class="php-hl-brackets">{</span>
        <span class="php-hl-reserved">return</span> <span class="php-hl-identifier">parent</span><span class="php-hl-code">::</span><span class="php-hl-identifier">model</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$className</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
    </span><span class="php-hl-brackets">}</span>
 
    <span class="php-hl-reserved">function</span> <span class="php-hl-identifier">getId</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-brackets">{</span>
        <span class="php-hl-reserved">return</span> <span class="php-hl-quotes">'</span><span class="php-hl-string">Book</span><span class="php-hl-quotes">'</span><span class="php-hl-code">.</span><span class="php-hl-var">$this</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">id</span><span class="php-hl-code">;
    </span><span class="php-hl-brackets">}</span>
 
    <span class="php-hl-reserved">function</span> <span class="php-hl-identifier">getPrice</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-brackets">{</span>
        <span class="php-hl-reserved">return</span> <span class="php-hl-var">$this</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">price</span><span class="php-hl-code">;
    </span><span class="php-hl-brackets">}</span>
<span class="php-hl-brackets">}</span></pre></div></div>

<h2>API</h2>

<h3>EShoppingCart::put($position, $quantity)</h3>

<p>Добавляет в корзину позицию товара в количестве $quantity.
Если позиция товара уже была в корзине, то данные модели обновляются, а количество увеличивается на $quantity</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-var">$book</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Book</span><span class="php-hl-code">::</span><span class="php-hl-identifier">model</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">findByPk</span><span class="php-hl-brackets">(</span><span class="php-hl-number">1</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">в корзине 1 позиция с id=1 в количестве 1 единица.</span>
<span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book</span><span class="php-hl-code">,</span><span class="php-hl-number">2</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">в корзине 1 позиция с id=1 в количестве 3 единицы.</span>
<span class="php-hl-var">$book2</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Book</span><span class="php-hl-code">::</span><span class="php-hl-identifier">model</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">findByPk</span><span class="php-hl-brackets">(</span><span class="php-hl-number">2</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book2</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">в корзине 2 позиции с id=1 и id=2</span></pre></div></div>

<h3>EShoppingCart::update($position, $quantity)</h3>

<p>Обновляет в корзине позицию товара.
Если позиция товара уже была в корзине, то данные модели обновляются, а количество установится в $quantity.
Если позиции не было в корзине, то она добавляется в ней.
Если установлено $quantity&lt;1, то позиция удаляется из корзины</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-var">$book</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Book</span><span class="php-hl-code">::</span><span class="php-hl-identifier">model</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">findByPk</span><span class="php-hl-brackets">(</span><span class="php-hl-number">1</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">в корзине 1 позиция с id=1 в количестве 1 единица.</span>
<span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">update</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book</span><span class="php-hl-code">,</span><span class="php-hl-number">2</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">в корзине 1 позиция с id=1 в количестве 2 единицы.</span></pre></div></div>

<h3>EShoppingCart::remove($key)</h3>

<p>Удаляет позицию из корзины</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-var">$book</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Book</span><span class="php-hl-code">::</span><span class="php-hl-identifier">model</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">findByPk</span><span class="php-hl-brackets">(</span><span class="php-hl-number">1</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book</span><span class="php-hl-code">,</span><span class="php-hl-number">2</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">в корзине 1 позиция с id=1 в количестве 2 единицы.</span>
<span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">remove</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">getId</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">в корзине нет позиций</span></pre></div></div>

<h3>EShoppingCart::clear()</h3>

<p>Очищает корзину</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">clear</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;</span></pre></div></div>

<h3>EShoppingCart::itemAt($key)</h3>

<p>Возвращает позицию по ключу</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-var">$position</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">itemAt</span><span class="php-hl-brackets">(</span><span class="php-hl-number">1</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;</span></pre></div></div>

<h3>EShoppingCart::contains($key)</h3>

<p>Возвращает boolean: есть ли в корзине позиция с id=$key?</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-var">$position</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">itemAt</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;</span></pre></div></div>

<h3>EShoppingCart::isEmpty()</h3>

<p>Возвращает true, если корзина пустая.</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-var">$position</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">isEmpty</span><span class="php-hl-brackets">(</span><span class="php-hl-number">1</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;</span></pre></div></div>

<h3>EShoppingCart::getСount()</h3>

<p>Возвращает количество позиций</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book</span><span class="php-hl-code">,</span><span class="php-hl-number">2</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book2</span><span class="php-hl-code">,</span><span class="php-hl-number">3</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">getСount</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">2</span></pre></div></div>

<h3>EShoppingCart::getItemsCount()</h3>

<p>Возвращает количество товаров</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book</span><span class="php-hl-code">,</span><span class="php-hl-number">2</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book2</span><span class="php-hl-code">,</span><span class="php-hl-number">3</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">getItemsCount</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">5</span></pre></div></div>

<h3>EShoppingCart::getCost($withDiscount)</h3>

<p>Возвращает стоимость всей корзины</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book</span><span class="php-hl-code">,</span><span class="php-hl-number">2</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">price=100</span>
<span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book2</span><span class="php-hl-code">,</span><span class="php-hl-number">1</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">price=200</span>
<span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">getCost</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">400</span></pre></div></div>

<h3>EShoppingCart::getPositions()</h3>

<p>Возвращает массив позиций</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-var">$positions</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">getPositions</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-reserved">foreach</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$positions</span> <span class="php-hl-reserved">as</span> <span class="php-hl-var">$position</span><span class="php-hl-brackets">)</span> <span class="php-hl-brackets">{</span><span class="php-hl-code">
...
</span><span class="php-hl-brackets">}</span></pre></div></div>

<h3>IECartPosition::getPrice()</h3>

<p>Возвращает цену одной единицы позиции</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-var">$positions</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">getPositions</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-reserved">foreach</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$positions</span> <span class="php-hl-reserved">as</span> <span class="php-hl-var">$position</span><span class="php-hl-brackets">)</span> <span class="php-hl-brackets">{</span>
<span class="php-hl-var">$price</span><span class="php-hl-code"> = </span><span class="php-hl-var">$position</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">getPrice</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-brackets">}</span></pre></div></div>

<h3>IECartPosition::getSumPrice($withDiscount)</h3>

<p>Возвращает стоимость позиции = стоимость одной единицы*кол-во</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-var">$book</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Book</span><span class="php-hl-code">::</span><span class="php-hl-identifier">model</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">findByPk</span><span class="php-hl-brackets">(</span><span class="php-hl-number">1</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">цена товара = 100</span>
<span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book</span><span class="php-hl-code">,</span><span class="php-hl-number">2</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">положим 2 единицы товара</span>
<span class="php-hl-var">$positions</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">getPositions</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-reserved">foreach</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$positions</span> <span class="php-hl-reserved">as</span> <span class="php-hl-var">$position</span><span class="php-hl-brackets">)</span> <span class="php-hl-brackets">{</span>
<span class="php-hl-var">$price</span><span class="php-hl-code"> = </span><span class="php-hl-var">$position</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">getSumPrice</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">200 (2*100)</span>
<span class="php-hl-brackets">}</span></pre></div></div>

<h3>IECartPosition::getQuantity()</h3>

<p>Возвращает кол-во единиц в позиции</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-var">$book</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Book</span><span class="php-hl-code">::</span><span class="php-hl-identifier">model</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">findByPk</span><span class="php-hl-brackets">(</span><span class="php-hl-number">1</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">цена товара = 100</span>
<span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book</span><span class="php-hl-code">,</span><span class="php-hl-number">2</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">положим 2 единицы товара</span>
<span class="php-hl-var">$positions</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">getPositions</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-reserved">foreach</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$positions</span> <span class="php-hl-reserved">as</span> <span class="php-hl-var">$position</span><span class="php-hl-brackets">)</span> <span class="php-hl-brackets">{</span>
<span class="php-hl-var">$price</span><span class="php-hl-code"> = </span><span class="php-hl-var">$position</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">getQuantity</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">2</span>
<span class="php-hl-brackets">}</span></pre></div></div>

<h2>Система скидок</h2>

<p>Для компонента корзины, реализована простая система скидок.</p>

<p>Система скидок позволяет применять к корзине набор правил для изменения стоимости как корзины в целом так и отдельных позиций.</p>

<p>Скидка - класс наследуемый от IEDiscount и реализующий метод apply.</p>

<p>В методе apply должен быть описан механизм применения скидки.</p>

<p>Применяется она путем вызова метода addDiscountPrice либо у позиции, либо у корзины( EShoppingCart::addDiscountPrice и Position::addDiscountPrice).
В метод передается значение на которое будет уменьшена стоимость(всей корзины или отдельной позиции соответственно).</p>

<p>Пример класса скидки:</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-reserved">class</span> <span class="php-hl-identifier">TestDiscount</span> <span class="php-hl-reserved">extends</span> <span class="php-hl-identifier">IEDiscount</span> <span class="php-hl-brackets">{</span>
    <span class="php-hl-comment">/*</span><span class="php-hl-comment">*
     * Скидка в %
     </span><span class="php-hl-comment">*/</span>
    <span class="php-hl-reserved">public</span> <span class="php-hl-var">$rate</span><span class="php-hl-code"> = </span><span class="php-hl-number">30</span><span class="php-hl-code">;
 
    </span><span class="php-hl-reserved">public</span> <span class="php-hl-reserved">function</span> <span class="php-hl-identifier">apply</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span> <span class="php-hl-brackets">{</span>
        <span class="php-hl-reserved">foreach</span> <span class="php-hl-brackets">(</span><span class="php-hl-var">$this</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span> <span class="php-hl-reserved">as</span> <span class="php-hl-var">$position</span><span class="php-hl-brackets">)</span> <span class="php-hl-brackets">{</span>
            <span class="php-hl-var">$quantity</span><span class="php-hl-code"> = </span><span class="php-hl-var">$position</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">getQuantity</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
            </span><span class="php-hl-reserved">if</span> <span class="php-hl-brackets">(</span><span class="php-hl-var">$quantity</span><span class="php-hl-code"> &gt; </span><span class="php-hl-number">1</span><span class="php-hl-brackets">)</span> <span class="php-hl-brackets">{</span>
                <span class="php-hl-var">$discountPrice</span><span class="php-hl-code"> = </span><span class="php-hl-var">$this</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">rate</span><span class="php-hl-code"> * </span><span class="php-hl-var">$position</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">getPrice</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code"> / </span><span class="php-hl-number">100</span><span class="php-hl-code">;
                </span><span class="php-hl-var">$position</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">addDiscountPrice</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$discountPrice</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
            </span><span class="php-hl-brackets">}</span>
        <span class="php-hl-brackets">}</span>
    <span class="php-hl-brackets">}</span>
<span class="php-hl-brackets">}</span></pre></div></div>

<p>Данная скидка делает следующее: если в корзину был добавлен товар в количестве больше 1, то к одной единице применяется скидка в $rate % , и на эту стоимость уменьшается суммарная стоимость позиции.</p>

<p>К корзине может быть применен не ограниченный набор скидок, которые будут вызываться по цепочке.</p>

<p>Список скидок должен быть описан в конфиге:</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-quotes">'</span><span class="php-hl-string">shoppingCart</span><span class="php-hl-quotes">'</span><span class="php-hl-code"> =&gt;
        </span><span class="php-hl-reserved">array</span><span class="php-hl-brackets">(</span>
            <span class="php-hl-quotes">'</span><span class="php-hl-string">class</span><span class="php-hl-quotes">'</span><span class="php-hl-code"> =&gt; </span><span class="php-hl-quotes">'</span><span class="php-hl-string">ext.yiiext.components.shoppingCart.EShoppingCart</span><span class="php-hl-quotes">'</span><span class="php-hl-code">,
            </span><span class="php-hl-quotes">'</span><span class="php-hl-string">discounts</span><span class="php-hl-quotes">'</span><span class="php-hl-code"> =&gt;
            </span><span class="php-hl-reserved">array</span><span class="php-hl-brackets">(</span>
                <span class="php-hl-reserved">array</span><span class="php-hl-brackets">(</span>
                    <span class="php-hl-quotes">'</span><span class="php-hl-string">class</span><span class="php-hl-quotes">'</span><span class="php-hl-code"> =&gt; </span><span class="php-hl-quotes">'</span><span class="php-hl-string">ext.yiiext.components.shoppingCart.discounts.TestDiscount</span><span class="php-hl-quotes">'</span><span class="php-hl-code">,
                    </span><span class="php-hl-quotes">'</span><span class="php-hl-string">rate</span><span class="php-hl-quotes">'</span><span class="php-hl-code"> =&gt; </span><span class="php-hl-number">40</span><span class="php-hl-brackets">)</span><span class="php-hl-code">,
                </span><span class="php-hl-reserved">array</span><span class="php-hl-brackets">(</span><span class="php-hl-quotes">'</span><span class="php-hl-string">class</span><span class="php-hl-quotes">'</span><span class="php-hl-code"> =&gt; </span><span class="php-hl-quotes">'</span><span class="php-hl-string">otherDiscount</span><span class="php-hl-quotes">'</span><span class="php-hl-brackets">)</span><span class="php-hl-code">,
            </span><span class="php-hl-brackets">)</span><span class="php-hl-code">,
 
        </span><span class="php-hl-brackets">)</span><span class="php-hl-code">,</span></pre></div></div>

<h2>События.</h2>

<p>В ShoppingCart зарегистрированы 2 события.
Механизм событий реализован согласно API Yii.</p>

<p>1) onUpdatePosition - срабатывает при добавлении или обновлении позиции в корзину.</p>

<p>2) onRemovePosition - срабатывает при удалении позиции из корзины.</p>

<p>Пример использования:</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-var">$cN</span><span class="php-hl-code"> = </span><span class="php-hl-reserved">new</span> <span class="php-hl-identifier">CallCenterNotifier</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">attachEventHandler</span><span class="php-hl-brackets">(</span><span class="php-hl-quotes">'</span><span class="php-hl-string">onUpdatePoistion</span><span class="php-hl-quotes">'</span><span class="php-hl-code">,</span><span class="php-hl-reserved">array</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$cN</span><span class="php-hl-code">, </span><span class="php-hl-quotes">'</span><span class="php-hl-string">updatePositionInShoppingCart</span><span class="php-hl-quotes">'</span><span class="php-hl-brackets">)</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
 
</span><span class="php-hl-var">$book</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Book</span><span class="php-hl-code">::</span><span class="php-hl-identifier">model</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">findByPk</span><span class="php-hl-brackets">(</span><span class="php-hl-number">1</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">put</span><span class="php-hl-brackets">(</span><span class="php-hl-var">$book</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;</span></pre></div></div>

<p>При срабатывание события onUpdatePoistion, об этом будет оповещен call center.</p>

<h2>Сmap - работаем с корзиной, как с массивом.</h2>

<p>Класс ShoppingCart наследует Сmap, это позволяет работать с корзиной как с массивом.</p>

<div class="hl-code"><div class="php-hl-main"><pre><span class="php-hl-var">$book</span><span class="php-hl-code"> = </span><span class="php-hl-identifier">Book</span><span class="php-hl-code">::</span><span class="php-hl-identifier">model</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">findByPk</span><span class="php-hl-brackets">(</span><span class="php-hl-number">1</span><span class="php-hl-brackets">)</span><span class="php-hl-code">;
</span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span><span class="php-hl-brackets">[</span><span class="php-hl-brackets">]</span><span class="php-hl-code"> = </span><span class="php-hl-var">$book</span><span class="php-hl-code">; </span><span class="php-hl-comment">//</span><span class="php-hl-comment">добавляем в корзину позицию.</span>
 
<span class="php-hl-comment">//</span><span class="php-hl-comment">обходим все позиции в корзине циклом.</span>
<span class="php-hl-reserved">foreach</span><span class="php-hl-brackets">(</span><span class="php-hl-identifier">Yii</span><span class="php-hl-code">::</span><span class="php-hl-identifier">app</span><span class="php-hl-brackets">(</span><span class="php-hl-brackets">)</span><span class="php-hl-code">-&gt;</span><span class="php-hl-identifier">shoppingCart</span> <span class="php-hl-reserved">as</span> <span class="php-hl-var">$position</span><span class="php-hl-brackets">)</span>
<span class="php-hl-brackets">{</span><span class="php-hl-code">
...
</span><span class="php-hl-brackets">}</span></pre></div></div>
        <hr />
        <p style="text-align: right;">© 2009 — 2010, <a href="http://code.google.com/p/yiiext/">yiiext team</a>.</p>        
    </div>
</body>
</html>
